package lhxia.translater.http.server;

import java.nio.ByteBuffer;
import java.util.concurrent.LinkedBlockingQueue;

public class BufferPool {
	
	private static BufferPool instance = new BufferPool();
	
	private LinkedBlockingQueue<ByteBuffer> pool = new LinkedBlockingQueue<ByteBuffer>();
	
	public static BufferPool getInstance(){
		return instance;
	}
	
	public ByteBuffer getBuffer(){
		ByteBuffer buffer = pool.poll();
		if(buffer == null){
			buffer = ByteBuffer.allocate(1024);
//			System.out.println("allocate");
		}else {
//			System.out.println("old");
		}
		buffer.clear();
		return buffer;
	}
	
	public void backBuffer(ByteBuffer buffer){
		try {
//			if(pool.size() < 1000){
				pool.put(buffer);
//			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
